package cn.com.controller;

import cn.com.domain.BacklogEntity;
import cn.com.domain.dto.BacklogDTO;
import cn.com.mapper.BacklogMapper;
import cn.com.service.BacklogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.com.Util.CopyUtils;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待办信息
 *
 * @Author CRL
 * @Date 2021/4/11 17:42
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/backlog")
public class BacklogController {

    @Autowired
    BacklogService backlogService;

    @Autowired
    BacklogMapper backlogMapper;

    @PostMapping
    public BaseResponse<BacklogEntity> creat(@RequestBody(required = false) BacklogEntity requestDto) {
        boolean save = backlogService.save(requestDto);
        if (save) {
            return new BaseResponse<>(requestDto);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    @DeleteMapping("/{id}")
    public BaseResponse delete(@PathVariable("id") String id) {
        int i = backlogMapper.deleteById(id);
        if (i == 1) {
            return new BaseResponse(BaseResponseStatus.OK, "删除成功");
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "删除失败");
        }
    }

    @PutMapping("/{id}")
    public BaseResponse update(@PathVariable("id") String id, @RequestBody(required = false) BacklogEntity requestDto) {
        Map<String, Object> map = new HashMap(1);
        map.put("id", id);
        List<BacklogEntity> backlogEntities = backlogMapper.selectByMap(map);
        BacklogEntity backlogEntity = backlogEntities.get(0);
        CopyUtils.copyProperties(requestDto, backlogEntity);
        boolean b = backlogService.saveOrUpdate(backlogEntity);
        if (b) {
            return new BaseResponse<>(requestDto);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "更新失败");
        }
    }

    @PostMapping("/query")
    public BaseResponse query(@RequestBody(required = false) BacklogDTO requestDto) {
        Integer pageNo = requestDto.getPageNo();
        Integer pageSize = requestDto.getPageSize();
        IPage<BacklogEntity> userPage = new Page<>(pageNo, pageSize);
        QueryWrapper<BacklogEntity> wrapper = new QueryWrapper<>();
        CopyUtils.copyProperties(requestDto, wrapper);
        IPage<BacklogEntity> backlogEntityIPage = backlogMapper.selectPage(userPage, wrapper);
        return new BaseResponse(backlogEntityIPage);
    }

    @GetMapping("/{id}")
    public BaseResponse findOneById(@PathVariable("id") String id) {
        BacklogEntity serviceById = backlogService.getById(id);
        return new BaseResponse(serviceById);
    }

}
